﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using UMC.Web.UI;
using UMC.Web;
using UMC.Wiki.Entities;
using System.Collections;

namespace UMC.Wiki.Activities
{



    class SubjectSelectActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {

            var key = this.AsyncDialog("Key", g => this.DialogValue("Promotion"));
            if (String.IsNullOrEmpty(request.SendValue) == false)
            {
                var buider = new UISectionBuilder(request.Model, request.Command, request.Arguments);
                buider.CloseEvent("UI.Event");
                this.Context.Send(buider.Builder(), true);
            }

            var user = this.Context.Token.Identity();
            var ui = UISection.Create();

            var paramsKey = request.SendValues ?? new UMC.Web.WebMeta();

            var start = UMC.Data.Utility.Parse((paramsKey["start"] ?? "0").ToString(), 0);
            var limit = UMC.Data.Utility.Parse((paramsKey["limit"] ?? "25").ToString(), 25);



            var Type = this.AsyncDialog("Type", "Project");
            var itemId = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Item", cId =>
            {
                return this.DialogValue("none");
            }));
            switch (Type)
            {
                case "Project":
                    {

                        ui.Title = new UITitle("我的专栏项目"); ;
                        var ids = new List<ulong>();
                        var pros = new List<Project>();
                        UMC.Data.Utility.Each(DataFactory.Instance().Projects(user.Id.Value, WebAuthType.Admin, WebAuthType.User), dr =>
                        {
                            pros.Add(dr); ids.Add(dr.Id.Value);
                        });


                        if (ids.Count > 0)
                        {
                            var webr = UMC.Web.WebResource.Instance();
                            foreach (var p in pros)
                            {
                                var desc = new UIIconNameDesc(new UIIconNameDesc.Item(webr.ImageResolve(request.Url, p.Id.Value, "1", "4"), p.Caption,
                                    String.Format("知识{0}篇", p.SubjectTotal ?? 0))
                                    .Click(Web.UIClick.Query(new WebMeta().Put("Item", p.Id).Put("Type", "Items"))));

                                if (p.user_id == user.Id)
                                {
                                    desc.Button("我的");
                                }
                                ui.Add(desc);

                            }
                        }
                        else
                        {
                            ui.Add("Desc", new UMC.Web.WebMeta().Put("desc", "你未有专栏的项目").Put("icon", "\uF016"), new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"),

                                new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));

                        }
                        response.Redirect(ui);
                    }
                    break;
                case "Items":
                    {
                        ui.Title = new UITitle("选择专栏");

                        var project = DataFactory.Instance().Project(itemId.Value);

                        ui.AddCell('\uf112', "返回上一级", project.Caption, new UIClick(new WebMeta().Put("Key", key).Put("Type", "Project")) { Key = "Query" });

                        var u3 = ui.NewSection();

                        UMC.Data.Utility.Each(DataFactory.Instance().ProjectItem(project), dr =>
                         {
                             u3.AddCell('\uf022', dr.Caption, String.Format("{0}篇", dr.SubjectTotal)
                              , new UIClick(new WebMeta().Put("Key", key).Put("Item", dr.Id).Put("Type", "Portfolio")) { Key = "Query" });
                         });
                    }
                    break;
                case "Portfolio":
                    {

                        ui.Title = new UITitle("选择文集");
                        var projectItem = DataFactory.Instance().ProjectItem(itemId.Value);

                        ui.AddCell('\uf112', "返回上一级", projectItem.Caption, new UIClick(new WebMeta().Put("Key", key).Put("Type", "Items").Put("Item", projectItem.project_id)) { Key = "Query" });

                        var ui3 = ui.NewSection();

                        UMC.Data.Utility.Each(DataFactory.Instance().Portfolio(projectItem), dr =>
                         {
                             ui3.AddCell('\uf22b', dr.Caption, String.Format("{0}篇", dr.SubjectTotal)
                                              , new UIClick(new WebMeta().Put("Key", key).Put("Item", dr.Id).Put("Type", "Subs")) { Key = "Query" });
                         });
                    }
                    break;
                case "Subs":
                    {
                        var ui3 = ui;
                        if (start == 0)
                        {
                            ui.Title = new UITitle("图文选择");
                            var portfolio = DataFactory.Instance().Portfolio(itemId.Value);

                            ui.AddCell('\uf112', "返回上一级", portfolio.Caption, new UIClick(new WebMeta().Put("Key", key).Put("Type", "Portfolio").Put("Item", portfolio.project_item_id)) { Key = "Query" });


                            ui3 = ui.NewSection();

                        }
                        ui3.Key = "Subs";
                        int nextIndex;
                        var subs = DataFactory.Instance().Search(new Subject { Status = 1, portfolio_id = itemId }, start, out nextIndex, limit);

                        SubjectUIActivity.Search(ui3, subs, request.Model, "UIData", false, request.IsApp, request.Url);
                        if (subs.Length == 0 && start == 0)
                        {
                            ui.IsNext = false;
                            ui3.Add("Desc", new UMC.Web.WebMeta().Put("desc", "尚未有发布的项目资讯").Put("icon", "\uF016")
                                , new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"), new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));

                        }
                        else
                        {
                            ui.IsNext = nextIndex > 0;
                            ui.StartIndex = nextIndex;
                        }



                    }
                    break;
                case "Sub":
                    {
                        var su = DataFactory.Instance().Subject(itemId.Value);

                        this.Context.UIEvent(key, new Web.ListItem(su.Title, su.Id.ToString()), true);

                    }
                    break;
            }
            response.Redirect(ui);

        }


    }
}